package com.daxia.ogmall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.ogmall.common.PayBizType;
import com.daxia.ogmall.dao.CompanyDAO;
import com.daxia.ogmall.dao.PayDAO;
import com.daxia.ogmall.dao.UserDAO;
import com.daxia.ogmall.dto.CompanyDTO;
import com.daxia.ogmall.dto.PayDTO;
import com.daxia.ogmall.model.City;
import com.daxia.ogmall.model.Company;
import com.daxia.ogmall.model.Pay;
import com.daxia.ogmall.model.User;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.util.BeanMapper;
import com.daxia.ogmall.util.SpringSecurityUtils;
import com.daxia.ogmall.util.ValidationUtils;

@Service
public class CompanyService {
	
	@Autowired
	private CompanyDAO companyDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private UserService userService;
	@Autowired
	private PayDAO payDAO;
	
	public List<CompanyDTO> find(CompanyDTO query, Page page) {
		List<Company> models = companyDAO.find(query, page);
		List<CompanyDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	 public List<CompanyDTO> findCompanyHavingPay(CompanyDTO query, Page page) {
		 List<Company> models = companyDAO.find(query, page);
		 List<CompanyDTO> dtos = toDTOs(models);
         for (CompanyDTO c : dtos) {
        	 PayDTO dto = new PayDTO();
        	 dto.setBizType(PayBizType.SubAccountRegister.getValue());
        	 dto.setUser(c.getUser());
        	 dto.setIsSuccess(true);
        	 Pay pay = payDAO.findOne(dto);
        	 if (null != pay) {
        		 dto = BeanMapper.map(pay, PayDTO.class);
        		 c.setPay(dto);
        	 }
         }
         return dtos;
    }
	
	public List<CompanyDTO> findAll() {
		return this.find(new CompanyDTO(), null);
	}
	
	public List<CompanyDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new CompanyDTO(), page);
	}
	
	public Long create(CompanyDTO dto) {
		Company model = new Company();
		toModel(model, dto);
		ValidationUtils.assertTrue(model.getUser() != null, "相关用户不存在");
		ValidationUtils.assertTrue(model.getUser().getCity() != null, "关联用户城市不能为空");
		Long id = companyDAO.create(model);
		model = companyDAO.load(id);
		User user = userDAO.load(model.getUser().getId());
		if (user.getParent() == null && SpringSecurityUtils.getCurrentUser() != null) {
			user.setParent(userService.findCityOperatorByCity(model.getUser().getCity().getId()));
			userDAO.update(user);
		}
		return id;
	}
	
	public CompanyDTO load(Long id) {
	    Company model = companyDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(CompanyDTO dto) {
		Company model = companyDAO.load(dto.getId());
		toModel(model, dto);
		companyDAO.update(model);
    }
	
	public void updateForAudit(CompanyDTO dto) {
		Company model = companyDAO.load(dto.getId());
		model.setAuditRemark(dto.getAuditRemark());
		model.setAuditStatus(dto.getAuditStatus());
		model.setAuditTime(dto.getAuditTime());
		model.setAuditUser(dto.getUser());
		companyDAO.update(model);
    }
	
	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				companyDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public CompanyDTO findOne(CompanyDTO query) {
		Company model = companyDAO.findOne(query);
		return toDTO(model);
	}
	
	public int updateCompanyStatuS(Long companyId,Integer status) {
		return companyDAO.updateCompanyStatuS(companyId, status);
	}
	
	public CompanyDTO findCompanyByUserId(Long userId) {
		Company company = companyDAO.findCompanyByUserId(userId);
		return toDTO(company);
	}
	
	private List<CompanyDTO> toDTOs(List<Company> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<CompanyDTO>(0);
		}
		List<CompanyDTO> dtos = new ArrayList<CompanyDTO>(models.size());
		for (Company model : models) {
	        CompanyDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private CompanyDTO toDTO(Company model) {
		if (model == null) {
			return null;
		}
		CompanyDTO dto = BeanMapper.map(model, CompanyDTO.class);
		
		return dto;
	}
	
	private void toModel(Company model, CompanyDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<Company> toModels(List<CompanyDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<Company>(0);
		}
		List<Company> models = new ArrayList<Company>(dtos.size());
		for (CompanyDTO dto : dtos) {
	        Company model = new Company();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}

	public List<CompanyDTO> findByCity(Long id) {
		CompanyDTO query = new CompanyDTO();
		query.setCity(new City());
		query.getCity().setId(id);
		return this.find(query, null);
	}
}
